<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        label.required:after { /* 给红色的必填项打红色星号（文字后）如果打前面用before*/
            content: ' *';
            color: red;
        }

        .layui-form-label {
            width: 125px;
            padding: 0px;
            text-align: left;
            height: 45px;
            line-height: 45px;
            font-size: 15px;
        }

        .layui-input-block {
            margin-left: 125px;
        }

        .layui-input {
            height: 45px;
            font-size: 15px;
        }

        .bottom {
            line-height: 0px;
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .button-container {
            position: absolute;
            right: 20px;
        }

        .pear-btn {
            border-radius: 4px;
            font-size: 18px;
        }

        .dtree-select {
            width: 99.5%;
            top: 46px
        }

        .layui-input[disabled] {
            color: black !important;
        }

        .layui-radio-disabled > i {
            color: #c2c2c2 !important;
        }

        .layui-radio-disabled > * {
            color: #070707 !important;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <div class="main-container">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label" style="display:none">id</label>
                <div class="layui-input-block" style="display:none">
                    <input type="text" th:value="${user_id}" id="user_id" name="user_id" autocomplete="off"
                           placeholder="请输入id" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="userName" lay-verify="required" lay-reqtext="请填写账号"
                           autocomplete="off" th:value="${user_name}" disabled
                           placeholder="请输入账号" class="layui-input layui-disabled">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="nickName" autocomplete="off" placeholder="请输入姓名" disabled
                           th:value="${nick_name}" class="layui-input layui-disabled">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" th:value="${email}" autocomplete="off"
                           placeholder="请输入邮箱" class="layui-input layui-disabled" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" th:value="${phone}" autocomplete="off"
                           placeholder="请输入电话" class="layui-input layui-disabled" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" th:checked="${ status == 1}? 'true':'false'"
                           value=true title="激活" disabled>
                    <input type="radio" name="status" th:checked="${ status == 2}? 'true':'false'"
                           value=false title="禁用" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">
                    部门
                </label>
                <div class="layui-input-block">
                    <input type="text" name="phone" th:value="${dept_name}" autocomplete="off"
                           class="layui-input layui-disabled" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    用户创建时间
                </label>
                <div class="layui-input-block">
                    <input type="text" name="phone" th:value="${create_time}" autocomplete="off"
                           class="layui-input layui-disabled" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    用户最近修改时间
                </label>
                <div class="layui-input-block">
                    <input type="text" name="phone" th:value="${update_time}" autocomplete="off"
                           class="layui-input layui-disabled" disabled>
                </div>
            </div>
            <input type="text" id="roleId" name="roleId" style="display:none;"/>
            <div class="layui-form-item">
                <label class="layui-form-label required">角色</label>
                <div class="layui-input-block show-role-container">
                </div>
            </div>

        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        //获取所有的角色列表
        getAllRole(function () {
            layui.form.render('radio')
        });
        form.on('submit(user-save)', function (data) {
            var rolelRadio = $("[name=roleId]:checked");
            if (rolelRadio == undefined || rolelRadio.length <= 0) {
                layer.alert("请给该用户设置角色", {icon: 5});
                return false;
            }
            let loading = layer.load();
            $.ajax({
                url: '/my/user/edit',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
            return false;
        });

        //获取所有的角色列表
        function getAllRole(callback) {
            $.ajax({
                url: "/my/role/all",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = "";
                    if (result.data && result.data.length > 0) {
                        for (i in result.data) {
                            html += '<input type="radio"  name="roleId" value="' + result.data[i].role_id + '" title="' + result.data[i].role_name + '" disabled>' +
                                '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><div>' + result.data[i].role_name + '</div></div>';
                        }
                        $(".show-role-container").html(html);
                        getCurrentUserRole(callback);
                    }
                }
            });
        }

        //当前用户关联的角色id
        function getCurrentUserRole(callback) {
            var userId = $("#user_id").val();
            $.ajax({
                url: "/my/role/user",
                type: "POST",
                data: JSON.stringify({
                    "userId": userId
                }),
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        $("[name=roleId]").each(function () {
                            if ($(this).val() == result.data.roleId) {
                                $(this).attr("checked", "checked");
                                return false;
                            }
                        })
                    } else {
                        layer.alert(result.message)
                    }
                    if (callback) {
                        callback();
                    }
                }
            });
        }
    })
</script>
<script>
</script>
</body>
</html>